package org.malred.v2.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * minicat主类
 */
public class Bootstrap {
    /**
     * socket监听的端口号
     */
    private int port = 8080;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    /**
     * minicat启动需要初始化展开的一些操作
     */
    public void start() throws Exception {
        /*
         * minicat v1.0,http://localhost:8080,返回固定字符串到页面
         */
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("minicat start on port: " + port);
        /*
         * 完成Minicat 2.0版本
         * 需求: 封装Request和Response对象,返回html静态资源文件
         */
        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            //封装request和response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            response.outputHtml(request.getUrl());
            socket.close();
        }
    }
}